<template>
  <view>
    <view class="search-box">
      <uni-search-bar @confirm="search" @input="input" cancelButton="none" :radius="20" :focus="true"></uni-search-bar>
      <!-- 搜索建议 -->
      <uni-list v-if="searchResults.length > 0">
        <uni-list-item
          :ellipsis="1"
          showArrow
          :title="item.goods_name"
          v-for="(item, index) in searchResults"
          :key="index"
          :to="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
        />
      </uni-list>
      <!-- 搜索历史 -->
      <view class="history-box" v-else>
        <view class="history-title">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="clearHistory" />
        </view>
        <view class="history-list"><uni-tag :text="item" v-for="(item, index) in histories" :key="index" @click="gotoGoodsList(item)" /></view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'SearchPage',
  components: {},
  data() {
    return {
      kw: '', //搜索内容
      searchResults: [], //搜索结果
      timer: null, //定时器名称
      historyList: uni.getStorageSync('kw') || [] //搜索历史
    };
  },
  created() {},
  mounted() {},
  onLoad() {},
  methods: {
    search() {},
    //清空历史记录
    clearHistory() {
      this.historyList = [];
      uni.setStorageSync('kw', []);
    },
    input(e) {
      this.kw = e;
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
      this.timer = setTimeout(() => {
        this.getSearchResults();
      }, 500);
    },
    async getSearchResults() {
      if (this.kw.trim() === '') {
        this.kw = '';
        this.searchResults = [];
        return;
      }
      let {
        data: {
          message,
          meta: {status}
        }
      } = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw});
      if (status !== 200) {
        return this.$showMsg();
      }
      this.searchResults = message;
      this.saveHistoryList();
    },
    //存储历史记录
    saveHistoryList() {
      //方法1：删除已有
      // let index = this.historyList.findIndex(t=?t===this.kw)
      // if(index>-1){
      //   this.historyList.splice(index,1)
      // }
      //  this.historyList.push(this.kw)
      //方式2：用set数据类型
      let set = new Set(this.historyList);
      set.delete(this.kw);
      set.add(this.kw);
      this.historyList = [...set];
      console.log(this.historyList)
      uni.setStorageSync('kw', this.historyList);
    },
    gotoGoodsList(query) {
      uni.navigateTo({
        url: '/subpkg/goods_list/goods_list?query=' + query
      });
    }
  },
  computed: {
    histories() {
      // 而是应该新建一个内存无关的数组，再进行 reverse 反转
      return [...this.historyList].reverse();
    }
  }
};
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0px;
  z-index: 999;
}
.history-box {
  padding: 0 5px;
  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 40px;
    font-size: 13px;
    border-bottom: 1px solid #efefef;
  }
  .history-list {
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
